package com.geralt.im.gui;

import geralt.im.watcher.Observer;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.geralt.im.commandparse.IParser;
import com.geralt.im.commandparse.impl.RpParser;
import com.geralt.im.controller.Controller;
import com.geralt.im.entrance.Entrance;
import com.geralt.im.model.Customer;
import com.geralt.im.model.Message;
import com.geralt.im.protocol.Request;
import com.geralt.im.protocol.Response;
import com.geralt.im.uicontroller.IUIListener;

public class LoginUI extends JFrame implements Observer {

	// private JFrame frame;
	private JTextField textField;
	private JLabel overallLabel;
	private JLabel loginLabel;
	private JPanel panel;
	private JTextField nametxt;
	private JPasswordField passwordtxt;
	private JButton login;
	private JLabel register;
	private IUIListener uiListener;
	public LoginUI() {
		initialize();
	}

	private void initialize() {
		
		
		setBounds(100, 100, 450, 450);
		setResizable(false);
		setLocationRelativeTo(null);

		this.addWindowListener(onCloseListener);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
		getContentPane().setBackground(Color.white);

		overallLabel = new JLabel();
		overallLabel.setIcon(new ImageIcon("headImg.gif"));
		overallLabel.setOpaque(false);
		overallLabel.setBounds(-130, -50, 704, 522);
		overallLabel.setBackground(null);

		getContentPane().add(overallLabel);

		loginLabel = new JLabel("L O G I N");
		loginLabel.setFont(new Font("Dialog", 1, 34));
		loginLabel.setHorizontalAlignment(JLabel.CENTER);
		loginLabel.setBounds(260, 97, 177, 86);
		overallLabel.add(loginLabel);

		panel = new JPanel();
		panel.setOpaque(false);
		panel.setBounds(215, 194, 278, 163);
		overallLabel.add(panel);
		panel.setLayout(null);

		nametxt = new TextFieldWithHint("username");
		nametxt.setColumns(10);
		nametxt.setBackground(Color.white);
		nametxt.setBounds(30, 34, 222, 35);
		panel.add(nametxt);

		passwordtxt = new PasswordWithHint("password");
		passwordtxt.setBackground(Color.white);
		passwordtxt.setBounds(30, 98, 222, 35);
		panel.add(passwordtxt);

		login = new JButton("sign  in");
		login.addMouseListener(loginBtnListener);
		passwordtxt.addKeyListener(passwordKeyListener);
		login.setFont(new Font("Dialog", 0, 24));
		login.setBounds(260, 369, 191, 42);
		overallLabel.add(login);

		register = new JLabel("<HTML><U>sign up</U></HTML>");
		register.setForeground(Color.blue);
		register.addMouseListener(registListener);
		register.setBounds(405, 422, 46, 23);
		overallLabel.add(register);
		// frame = new JFrame();
//		setBounds(100, 100, 450, 450);
//		setResizable(false);
//		setLocationRelativeTo(null);
//		setBackground(Color.black);
//
//		this.addWindowListener(onCloseListener);
//
//		setDefaultCloseOperation(EXIT_ON_CLOSE);
//		getContentPane().setLayout(null);
//
//		overallLabel = new JLabel();
//		overallLabel.setIcon(new ImageIcon("head4.gif"));
//		overallLabel.setOpaque(false);
//		overallLabel.setBounds(0, 0, 444, 422);
//		overallLabel.setBackground(null);
//
//		getContentPane().add(overallLabel);
//
//		loginLabel = new JLabel("L O G I N");
//		loginLabel.setFont(new Font("Dialog", 1, 34));
//		loginLabel.setHorizontalAlignment(JLabel.CENTER);
//		loginLabel.setBounds(130, 47, 177, 86);
//		overallLabel.add(loginLabel);
//
//		panel = new JPanel();
//		panel.setOpaque(false);
//		panel.setBounds(85, 144, 278, 163);
//		overallLabel.add(panel);
//		panel.setLayout(null);
//
//		nametxt = new TextFieldWithHint("username");
//		nametxt.setColumns(10);
//		nametxt.setBackground(Color.white);
//		nametxt.setBounds(30, 34, 222, 35);
//		panel.add(nametxt);
//
//		passwordtxt = new PasswordWithHint("password");
//		passwordtxt.setBackground(Color.white);
//		passwordtxt.setBounds(30, 98, 222, 35);
//		panel.add(passwordtxt);
//
//		login = new JButton("sign  in");
//		login.addMouseListener(loginBtnListener);
//		passwordtxt.addKeyListener(passwordKeyListener);
//		login.setFont(new Font("Dialog", 0, 24));
//		login.setBounds(130, 319, 191, 42);
//		overallLabel.add(login);
//
//		register = new JLabel("<HTML><U>sign up</U></HTML>");
//		register.setForeground(Color.blue);
//		register.addMouseListener(registListener);
//		register.setBounds(275, 372, 46, 23);
//		overallLabel.add(register);
	}

	private WindowListener onCloseListener = new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent e) {
			// if (Entrance.client.flag != false) {
			// Request request = new Request();
			// request.setType("EXIT");
			// Customer customer = new Customer();
			// request.getParameter().put("LOGIN", customer);
			// Entrance.client.getClientRunner().write(request);
			// }
		}
	};

	private MouseListener loginBtnListener = new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
			String name = nametxt.getText();
			String pwd = new String(passwordtxt.getPassword());
			if (name == null || name.length() == 0 || name.length() > 20) {
				JOptionPane.showMessageDialog(null,
						"the username you have entered is invalid",
						"username invalid", JOptionPane.ERROR_MESSAGE);
			} else {

				try {
					Request request = new Request();
					request.setType("LOGIN");
					Entrance.customer = new Customer();
					Entrance.customer.setId(name);
					Entrance.customer.setPassword(pwd);
					request.getParameter().put("LOGIN", Entrance.customer);
					Entrance.client.getClientRunner().write(request);
					Response response;
					response = Entrance.client.getClientRunner().read();
				} catch (Exception e1) {
					// e1.printStackTrace();
					JOptionPane.showMessageDialog(null,
							"Can't connect to Server");
				}
			}
			// System.out.println("name: " + nametxt.getText());
			// System.out.println("pwd: " + pwd);
		}
	};

	private KeyListener passwordKeyListener = new KeyAdapter() {
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				System.out.println("you just clicked enter!");
			}
		}

		public void keyReleased(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				System.out.println("you just clicked enter!");
			}
		}
	};

	private MouseListener registListener = new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
			uiListener.showUI("register");
			setVisible(false);

		}
	};
	
	@Override
	public void update(int type, Object object) {
		// System.out.println("Update +"+((Message) object).getContent());
		// /////////////
		if (Entrance.onLogin == true)
			return;
		switch (type) {
		case 1: // BadRequestCommand
			if("shutdown".equals(((Message) object).getContent())){
				JOptionPane.showMessageDialog(this,
						"Server shutdown,Program shutdown", "Error",
						JOptionPane.ERROR_MESSAGE);
				System.exit(1);
			}
		case 4: // LoginSuccess
			setVisible(false);
			Entrance.onLogin = true;
			Entrance.client.start();
			break;
		case 5:
			setVisible(true);
			nametxt.setText(Entrance.customer.getId());
			// passwordtxt.setText(Entrance.customer.getPassword());
		default:
			break;
		}
	}
	public void addIUIListener(IUIListener uiListener){
		this.uiListener = uiListener;
	}
}
